package com.heima.datasync.service.impl;

import com.heima.common.exception.CustException;
import com.heima.datasync.mapper.ApArticleMapper;
import com.heima.datasync.service.EsDataService;
import com.heima.es.service.EsService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.vos.SearchArticleVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class EsDataServiceImpl implements EsDataService {
    @Autowired
    EsService<SearchArticleVO> esService;
    @Autowired
    ApArticleMapper apArticleMapper;

    public static final String ARTICLE_INDEX_NAME = "app_info_article";


    @Override
    public ResponseResult dataInit() {
        // 1. 先判断索引库是否存在
        boolean isHas = esService.existIndex(ARTICLE_INDEX_NAME);
        // 2. 如果不存在  就创建索引库
        if(!isHas){
            esService.createIndex("{\n" +
                    "    \"mappings\":{\n" +
                    "        \"properties\":{\n" +
                    "            \"id\":{\n" +
                    "                \"type\":\"long\"\n" +
                    "            },\n" +
                    "            \"publishTime\":{\n" +
                    "                \"type\":\"date\"\n" +
                    "            },\n" +
                    "            \"layout\":{\n" +
                    "                \"type\":\"integer\"\n" +
                    "            },\n" +
                    "            \"images\":{\n" +
                    "                \"type\":\"keyword\",\n" +
                    "                \"index\": false\n" +
                    "            },\n" +
                    "           \"staticUrl\":{\n" +
                    "                \"type\":\"keyword\",\n" +
                    "                \"index\": false\n" +
                    "            },\n" +
                    "            \"authorId\": {\n" +
                    "          \t\t\"type\": \"long\"\n" +
                    "       \t\t},\n" +
                    "          \"title\":{\n" +
                    "            \"type\":\"text\",\n" +
                    "            \"analyzer\":\"ik_smart\"\n" +
                    "          }\n" +
                    "        }\n" +
                    "    }\n" +
                    "}",ARTICLE_INDEX_NAME);
        }
        // 3. 去article数据库中 查询满足条件的文章数据
        List<ApArticle> articleList = apArticleMapper.findAllArticles();
        if (CollectionUtils.isEmpty(articleList)) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"未查询到相关文章数据");
        }

        // 4. 将所有 ApArticle 转化为  SearchArticleVO
        List<SearchArticleVO> articleVoList = articleList.stream().map(article -> {
            SearchArticleVO searchArticleVO = new SearchArticleVO();
            BeanUtils.copyProperties(article, searchArticleVO);
            return searchArticleVO;
        }).collect(Collectors.toList());
        // 5. 将文章集合 批量的导入到es索引库中
        esService.saveBatch(articleVoList,ARTICLE_INDEX_NAME);
        return ResponseResult.okResult();
    }
}
